package com.xxkk.gameCore.global;

import com.ebo.synframework.nettybase.codec.ProtoFactory;
import com.xxkk.gameCore.enums.PlatformType;
import com.xxkk.gameCore.enums.ServerType;
import com.xxkk.gameCore.global.globalServer.ServerConfig;
import com.xxkk.gameCore.global.globalServer.ServerContext;
import com.xxkk.gameCore.global.globalServer.ServerData;
import com.xxkk.gameCore.net.redis.RoleRepo;
import com.xxkk.gameCore.net.sql.pojo.ServerInfo;
import com.xxkk.logic.LogicBoot;
import com.xxkk.utils.MysqlSyncUtil;
import com.xxkk.utils.ResourcesRootPathUtil;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;


public class GlobalBoot extends HttpServlet {
    private final static Logger logger = Logger.getLogger(GlobalBoot.class);
    private ApplicationContext ac;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            String url = "jdbc:mysql://192.168.31.88:3306/Himeka";
            String username = "root";
            String password = "123456";
//            MysqlSyncUtil.update(url, username, password);
            logger.info("启动成功");
            logger.info("rootPath:" + ResourcesRootPathUtil.getResourcePath());
            logger.info("user.dir" + ResourcesRootPathUtil.getUserDir());


            ProtoFactory.init("com.xxkk.gameCore.proto");
            ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
            ServerData serverData = ac.getBean(ServerData.class);// mysql服务器数据读取并初始化
            serverData.init();

            ServerContext serverContext = ac.getBean(ServerContext.class);
            serverContext.init();// TODO

            PlatformConfig platformConfig = ac.getBean(PlatformConfig.class);
            //serverConfig 一个服务器静态配置类 TODO il8n 多语言支持需要excel表，但读取excel组件代码还没时间去实现
            ServerConfig.platformType = PlatformType.valueOf(platformConfig.getPlatformType());
            logger.info("平台：" + ServerConfig.platformType.name() + "#初始值：" + ServerConfig.platformType.code);
//             BusinessPool businessPool = ac.getBean(BusinessPool.class);  定时任务暂未使用

            ServerInfo serverInfo = serverData.getSelfServerInfo();
            if (ServerType.valueOf(serverInfo.getServerType()) == ServerType.all) {// 根据服务器类型启动服务模块
                LogicBoot.start(ac);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
